# -*- coding: utf-8 -*-
import traceback

import scrapy
from scrapy.utils.response import open_in_browser
from scrapy.loader import ItemLoader

from MySpider.items import DataItem


class MySpider(scrapy.Spider):
    '''
    爬取主类
    '''
    name = "mySpider"

    # 爬取地址
    search_url = 'https://movie.douban.com/subject/%s/'

    def __init__(self, douban_id):
        super(MySpider, self).__init__()
        self.douban_id = douban_id
        self.spiderUrl = MySpider.search_url % self.douban_id

    '''
    # 入口函数: scrapy crawl mySpider
    '''
    def start_requests(self):
        try:
            yield scrapy.Request(url=self.spiderUrl, callback=self.parse)
        except Exception as e:
            print(traceback.print_exc())

    '''
    解析返回数据
    '''
    def parse(self, response):
        # open_in_browser(response)  # 在浏览器中显示爬取的结果
        loader = ItemLoader(item=DataItem(), response=response)
        loader.add_value('video', self.douban_id)
        try:

            loader.add_xpath("url1_name", '//*[@id="content"]/div[2]/div[2]/div[1]/ul/li[1]/a/attribute::data-cn')
            loader.add_xpath("url1_value", '//*[@id="content"]/div[2]/div[2]/div[1]/ul/li[1]/a/attribute::href')

            loader.add_xpath("url2_name", '//*[@id="content"]/div[2]/div[2]/div[1]/ul/li[2]/a/attribute::data-cn')
            loader.add_xpath("url2_value", '//*[@id="content"]/div[2]/div[2]/div[1]/ul/li[2]/a/attribute::href')

            loader.add_xpath("url3_name", '//*[@id="content"]/div[2]/div[2]/div[1]/ul/li[3]/a/attribute::data-cn')
            loader.add_xpath("url3_value", '//*[@id="content"]/div[2]/div[2]/div[1]/ul/li[3]/a/attribute::href')


            yield loader.load_item()
        except Exception as e:
            print(traceback.print_exc())